package com.zhiqianbao.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.DownloadListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.zhiqianbao.R;
import com.zhiqianbao.utils.Constants;

/**
 * Created by liang on 2016/1/8.
 */
public class RecommendAppActivity extends BaseActivity {
    private static final String LOGTAG = "recommendedAppActivity";
    WebView mWebView;
    TextView textviewRegisterTitle;

    String url;
    String title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recommended_app);
        Intent intent = getIntent();

        url = (String) intent.getSerializableExtra("url");
        title = (String) intent.getSerializableExtra("title");

        init();
        initView();
    }

    private void initView() {
         mWebView=(WebView)findViewById(R.id.mWebView);
         textviewRegisterTitle=(TextView)findViewById(R.id.textview_register_title);
        WebSettings webSettings = mWebView.getSettings();
        //设置WebView属性，能够执行Javascript脚本
        webSettings.setJavaScriptEnabled(true);
        //设置可以访问文件
        webSettings.setAllowFileAccess(false);
        //设置支持缩放
        webSettings.setBuiltInZoomControls(false);
        webSettings.setFixedFontFamily("Microsoft YaHei");

        //加载需要显示的网页
        mWebView.loadUrl(Constants.API_ROOT + url);

        mWebView.requestFocus();

        //设置Web视图
        mWebView.setWebChromeClient(new WebChromeClient());
        mWebView.setWebViewClient(new WebViewClient());
        //与js交互
        mWebView.setDownloadListener(new DownloadListener() {
            @Override
            public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
                Uri uri = Uri.parse(url);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
            }
        });
        textviewRegisterTitle.setText(title);
    }
    // 如果不做任何处理，浏览网页，点击系统“Back”键，整个Browser会调用finish()而结束自身，
    // 如果希望浏览的网 页回退而不是推出浏览器，需要在当前Activity中处理并消费掉该Back事件。
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if((keyCode==KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()){
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode,event);
    }

   /* @JavascriptInterface
    public void onDownload(String url) {
        Log.i(LOGTAG, "url =" + url);
        showShortToast(url);
    }*/

}
